// pages/subrogation/moneyPay/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '信用保证余额充值',
            'color': true,
            'class': false
        },
        pitchOn: 2000,
        inputOn: false,
        payOn: 1,
        dialogOne: false,
        dialogTwo: false,
        dialogThree: false,
        userInfo: wx.getStorageSync('userInfo')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        var that = this;
        wx.getStorage({
            key: 'chatInfo',
            success(res) {
                console.log(res)
                that.setData({
                    chatName: res.data.userInfo.nickName,
                    chatHead: res.data.userInfo.avatarUrl,
                    pitchOn: options.money ? options.money : 2000
                })
            }
        })
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    userInfo: res.data,
                    salesmanId: res.data.id
                })
                that.creditMargin()
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    // 获取信用保证金内容
    creditMargin(e) {
        var that = this;
        // /bdb/WorkerBee/creditMargin
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'creditMargin',
            q: {
                salesmanId: that.data.salesmanId
            }
        }), (res) => {}, res => {
            that.setData({
                guarantee: res.data
            })
        })
    },
    // 充值
    ensureRecharge(e) {
        var that = this,
            data = {
                "transactionMoney": that.data.pitchOn,
                "deptId": that.data.userInfo.id,
                "deptName": that.data.userInfo.nickName,
                "deptType": that.data.userInfo.userType,
                "operatorId": that.data.userInfo.id,
                "operatorName": that.data.userInfo.nickName
            }
        // /fms/accountsRechargeOrder/ensureRecharge
        app.basePost(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'ensureRecharge'
        }), data, res => {
            var payInfo = JSON.parse(res.data.payInfo),
                id = res.data.order_no;
            wx.requestPayment({
                timeStamp: payInfo.timeStamp,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                signType: payInfo.signType,
                paySign: payInfo.paySign,
                success(res) {
                    that.queryRecharge(id)
                },
                fail(res) {
                    that.cancelecharge(id)
                }
            })
        }, (res) => {})
    },
    // 查询充值订单详情
    queryRecharge(id) {
        var that = this;
        // /fms/accountsRechargeOrder/queryRechargeOrderInfo
        app.baseGet(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'queryRechargeOrderInfo',
            q: {
                orderno: id
            }
        }), res => {
            console.log(res)
            that.setData({
                orderInfo: res.data,
                dialogTwo: false,
                dialogOne: true
            })
        }, (res) => {})
    },
    // 取消保证金充值
    cancelecharge(id) {
        var that = this;
        // /fms/accountsRechargeOrder/cancelechargeOrder
        app.baseGet(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'cancelechargeOrder',
            q: {
                orderno: id
            }
        }), res => {
            console.log(res)
            that.setData({
                orderInfo: res.data,
                dialogTwo: true
            })
        }, (res) => {})
    },

    //好友代付生成订单
    indent() {
        var that = this,
            data = {
                "transactionMoney": that.data.pitchOn,
                "deptId": that.data.userInfo.id,
                "deptName": that.data.userInfo.nickName,
                "deptType": that.data.userInfo.userType,
                "operatorId": that.data.userInfo.id,
                "operatorName": that.data.userInfo.nickName
            };
        // /fms/accountsRechargeOrder/helpRecharge
        app.basePost(app.U({
            m: 'fms',
            c: 'accountsRechargeOrder',
            a: 'helpRecharge'
        }), data, res => {
            that.setData({
                orderNo: res.data.orderNo
            })
            wx.navigateTo({
                url: '/pages/subrogation/gfPayment/index?type=1&title=' + this.data.pitchOn + '&orderNo=' + this.data.orderNo,
            })
        }, (res) => {})
    },

    //取消订单
    cancelOrder() {
        this.setData({
            dialogThree: true
        })
    },
    //付款
    payment() {
        if (this.data.payOn == 1) {
            this.ensureRecharge()
        } else {
            this.getUserProfile()
        }
    },

    getUserProfile() {
        wx.getUserProfile({
            desc: '获取微信头像和昵称',
            success: (res) => {
                console.log("获取用户信息成功", res)
                wx.setStorageSync('chatInfo', res)
                this.setData({
                    chatName: res.userInfo.nickName,
                    chatHead: res.userInfo.avatarUrl
                });
                this.indent()
            },
            fail: res => {
                wx.showToast({
                    icon: 'none',
                    title: '获取用户信息失败'
                })
                console.log("获取用户信息失败", res)
            }
        })
    },
    //选择充值金额
    rechargeAmount(e) {
        console.log(e)
        var index = e.currentTarget.dataset.current
        this.setData({
            pitchOn: index
        })
    },
    //选择自定义充值金额
    customRecharge(e) {
        console.log(e)
        this.setData({
            inputOn: this.data.inputOn ? false : true,
            pitchOn: e.detail.value
        })
    },
    //选择支付方式
    paymentMethod(e) {
        console.log(e)
        var index = e.currentTarget.dataset.current
        this.setData({
            payOn: index
        })
    },
    //关闭充值详情
    dialogOne() {
        this.setData({
            dialogOne: false,
            dialogTwo: false,
            dialogThree: false
        })
    }
})